//
//  CustomTabBarController.m
//  RRIsSmallPig
//
//  Created by K。 on 15/7/21.
//  Copyright (c) 2015年 寇广超. All rights reserved.
//

#import "CustomTabBarController.h"

@interface CustomTabBarController ()
      
@end

@implementation CustomTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
        self.title = @"We are 伐木累";
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"抽屉"
                                                                                 style:UIBarButtonItemStylePlain
                                                                                target:self
                                                                                action:@selector(presentLeftMenuViewController:)];
        
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"分类"
                                                                             style:UIBarButtonItemStylePlain
                                                                            target:self
                                                                            action:@selector(rightBarButtonAction)];
        self.view.backgroundColor = [UIColor colorWithRed:0.4 green:0.5 blue:0.6 alpha:1.0];
    MealMenuController *menu = [[MealMenuController alloc]init];
    [self addChildViewController:menu title:@"菜谱" Image:nil selecetImage:nil];
    
    JIngpinController *JInPin = [[JIngpinController alloc]init];
    [self addChildViewController:JInPin title:@"精品汇" Image:nil selecetImage:nil];
    
    HotWaterController *hotWater = [[HotWaterController alloc]init];
    [self addChildViewController:hotWater title:@"热卖汤" Image:nil selecetImage:nil];
    
    VideoTeachController *video = [[VideoTeachController alloc]init];
    [self addChildViewController:video title:@"视频教学" Image:nil selecetImage:nil];
    
    CustomTabBar *tabBar = [[CustomTabBar alloc]init];
    tabBar.myPlay = self;
    [self setValue:tabBar forKey:@"tabBar"];

    
//***首页滚动
    UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc]init];
    flowlayout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height -49 -64) ;
    flowlayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowlayout.minimumInteritemSpacing = 0;
    flowlayout.minimumLineSpacing = 0;
    //***水平方向默认的
    flowlayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.scrollView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height -49 -64) collectionViewLayout:flowlayout];
    self.scrollView.backgroundColor = [UIColor whiteColor];
    self.scrollView.pagingEnabled = YES;
    self.scrollView.delegate = self;
    self.scrollView.dataSource = self;
    
    [self.view addSubview:self.scrollView];
    [self.scrollView registerClass:[ScrollViewCell class] forCellWithReuseIdentifier:@"resuse"];
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载";
    [self.HUD show:YES];
    self.page = 1;
    [self getData];
    

}
-(void)getData
{
    NSString *str = [NSString stringWithFormat:@"http://121.41.84.91:80/HandheldKitchen/api/more/tblcalendaralertinfo!getHomePage.do?phonetype=2&page=%ld&pageRecord=10&user_id=&is_traditional=0",self.page];
    NSLog(@"url ==  %@",str);
    self.scrollArray = [NSMutableArray array];
   [AFN_block NSString:str Blokc:^(id request) {
       NSMutableArray *array = [request objectForKey:@"data"];
       for (NSMutableDictionary *dic in array) {
           ScrollModle *scrModles = [[ScrollModle alloc]init];
           [scrModles setValuesForKeysWithDictionary:dic];
           [self.scrollArray addObject:scrModles];
       }
       [self.scrollView reloadData];
       [self.HUD hide:YES];
   }];

}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return  1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSLog(@"array == %ld",self.scrollArray.count);
    return  self.scrollArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    self.cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"resuse" forIndexPath:indexPath];
    _cell.scrModle = [self.scrollArray objectAtIndex:indexPath.row];
    [_cell.bigImageView sd_setImageWithURL:[NSURL URLWithString:_cell.scrModle.imagePathLandscape] placeholderImage:nil];
    _cell.englishNameLabel.text = _cell.scrModle.englishName;
    _cell.nameLabel.text = _cell.scrModle.name;
    
    return self.cell;
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger page = (scrollView.contentOffset.x/[UIScreen mainScreen].bounds.size.width);
    NSInteger pageControl = page%10;
    NSLog(@"****** ==  %ld",pageControl);
    [self.cell pageControlPage:pageControl];

}
//***左侧加载
-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x >9*[UIScreen mainScreen].bounds.size.width) {
        self.page++;
        [self getData];
        [self.HUD show:YES];
    }
    
 
}



























-(void)Present
{
    WangYaRongViewController *wry = [[WangYaRongViewController alloc]init];
    UINavigationController *naVC = [[UINavigationController alloc]initWithRootViewController:wry];
    [self presentViewController:naVC animated:YES completion:^{
        
        
    }];
    
}
-(void)rightBarButtonAction
{
    SortController *sortVC = [[SortController alloc]init];
    [self.navigationController pushViewController:sortVC animated:YES];

}
-(void)addChildViewController:(UIViewController *)childController title:(NSString *)title Image:(NSString *)image selecetImage:(NSString *)selectImage
{
        
        
        childController.title = title;
        childController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"右键" style:UIBarButtonItemStyleDone target:self action:@selector(rightButtonAction)];
        
        childController.tabBarItem.image = [UIImage imageNamed:image];
        childController.tabBarItem.selectedImage = [[UIImage imageNamed:selectImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        NSMutableDictionary *text = [NSMutableDictionary dictionary];
        text[NSForegroundColorAttributeName] = [UIColor blueColor];
        NSMutableDictionary *selecctText = [NSMutableDictionary dictionary];
        selecctText[NSForegroundColorAttributeName] = [UIColor orangeColor];
        
        [childController.tabBarItem setTitleTextAttributes:text forState:UIControlStateNormal];
        [childController.tabBarItem setTitleTextAttributes:selecctText forState:UIControlStateSelected];
        CustomNavigationController *Navigation = [[CustomNavigationController alloc]initWithRootViewController:childController];
        Navigation.navigationBar.translucent = NO;
        [self addChildViewController:Navigation];
        
    }
-(void)rightButtonAction
    {
        
        
    }
    
- (void)viewWillAppear:(BOOL)animated
{
        [super viewWillAppear:animated];
        NSLog(@"DEMOFirstViewController will appear");
}
    
- (void)viewWillDisappear:(BOOL)animated
{
        [super viewWillDisappear:animated];
        NSLog(@"DEMOFirstViewController will disappear");
}
    

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
